package com.ufm.patient.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ufm.patient.pojo.Patient;
import com.ufm.patient.service.IPatientService;
import com.ufm.patient.util.PatientSearch;

@Controller
@RequestMapping("/patient")
public class PatientController {
	@Autowired
	private IPatientService patientService;
	
	
	@RequestMapping(value="/findAll")
	public String findAll(Model model,HttpServletRequest request,PatientSearch ps) {
		Map param = new HashMap();
		param.put("name", ps.getName());
		param.put("idCard", ps.getIdCard());
		param.put("address", ps.getAddress());
		param.put("age", ps.getAge());
		param.put("contactNumber", ps.getContactNumber());
		param.put("dragAllergy", ps.getDragAllergy());
		param.put("fromDate", ps.getFromDate());
		param.put("gender", ps.getGender());
		param.put("medicalInsType", ps.getMedicalInsType());
		param.put("qqNumber", ps.getQqNumber());
		param.put("self_condition", ps.getSelf_condition());
		param.put("toDate", ps.getToDate());
		
		List<Patient> patients = patientService.findAll(param);
		model.addAttribute("patients",patients);
		model.addAttribute("patientSearch",ps);
		return "/patient/list";
	}
	@RequestMapping(value="/save")
	public String save(Model model,HttpServletRequest request,Patient patient) {		
		if(patient.getId()!=null){
			patientService.update(patient);
		}else{
			patientService.insert(patient);
		}
		
		return "redirect:/patient/findAll";
	}
	
	@RequestMapping(value="/ajaxView")
	public  String view(Model model,HttpServletRequest request,Integer id) {		
		Patient patient = patientService.findById(id);
		model.addAttribute("patient", patient);
		return "/patient/view";
	}
	
}
